<?php

ini_set('memory_limit','16M');

require_once( '/home/cyberpower678/Peachy/Init.php' );

$site = Peachy::newWiki( "cyberbot i" );

$site->set_runpage("User:Cyberbot I/Run/Uncat");

$template = initPage( "Template:Uncategorized" );
$p = array();

$ignorepage = initPage( "User:Cyberbot I/Uncat-ignore.js" )->get_text();

$ignorelist = array_filter(explode("\n",$ignorepage));
/*$ignorelist = array(
        'Articles lacking sources (Erik9bot)',
        'Articles created via the Article Wizard',
        'Unreviewed new articles',
);*/

$p = $template->embeddedin( array(0) );

$c = 1;

$tofind = array(
        'Classify',
        'CatNeeded',
        'Uncategorised',
        'Uncat',
        'Categorize',
        'Categories needed',
        'Categoryneeded',
        'Category needed',
        'Category requested',
        'Categories requested',
        'Nocats',
        'Categorise',
        'Nocat',
        'Uncat-date',
        'Uncategorized-date',
        'Needs cat',
        'Needs cats',
        'Cat needed',
        'Cats needed',
);

foreach ($p as $pg) {
        if( $c > 30 ) break;
       
        $page = initPage($pg);

        $text = $page->get_text();
        preg_match_all('/\[\[Category:(.*?)(\|(.*?))?\]\]/Si', $text, $cats);
        if( $cats ) {
                $cats = $cats[1];
                $remove = 'no';
                foreach( $cats as $cat ) {
                        if( in_array( $cat, $ignorelist ) ) {
                                continue;
                        }
                       
                        $tmp = initPage( 'Category:'.$cat );
                       
                        if( $tmp->get_exists() ) {
                                $remove = 'yes';
                                break;
                        }
                }
                if( $remove == 'yes' ) {
                        $newtext = preg_replace('/\{\{('.implode('|',$tofind).')(.*?)\}\}/i', '', $text);
                        $diff = getTextDiff('unified', $text, $newtext);
                        echo $diff;
                       
                        $page->edit($newtext,"Removing categorization template",true);
                        $c++;
                }
        }
}

?>


